using System;
using System.Collections.Generic;
using System.Linq;

namespace MapTeams.Umbraco.Polls.Dto
{
    /// <summary>
    /// Data structure to represent single Poll with all the poll choices including votes for each choice and total votes.
    /// 
    /// UI templating logic is also added so Mustache templating engine can differetiate on what section to render
    /// 
    /// Author: 
    /// Indra Kurniawan
    /// MAP Teams (http://www.mapits.com)
    /// </summary>
    public class MapTeamsPolls
    {
        public int PollID;
        public string Question;
        public string BlockMode;

        private string _cssPrefix = "MapTeams-";


        /// <summary>
        /// for UI templating logic; set it manually
        /// </summary>
        public bool IsAlreadyVoted = true;
        
        public string CssPrefix
        {
            get { return _cssPrefix; }
            set
            {
                if(string.IsNullOrEmpty(value))
                    return;
                _cssPrefix = value;
            }
        }

        public int TotalVotes = -1;

        public List<MapTeamsPollsChoice> Choices = new List<MapTeamsPollsChoice>();

        /// <summary>
        /// Calculate total votes, and percentage for each Poll choice 
        /// </summary>
        public void CalculateAllPercentage()
        {
            TotalVotes = Choices.Sum(o => o.VoteCount);
            
            if (TotalVotes > 0)
            {
                foreach (var choice in Choices)
                {
                    choice.Percentage = decimal.Round((decimal.Parse(choice.VoteCount.ToString()) / decimal.Parse(TotalVotes.ToString())) * 100, MidpointRounding.AwayFromZero);
                }
            }
        }
    }
}